package cn.swenty.backstage.base.web.index.controller;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import cn.swenty.backstage.framework.common.Global;
import cn.swenty.backstage.framework.common.model.Function;


@Controller
@RequestMapping("/")
public class IndexController {
	
	
	@RequestMapping("index")
	public String index(Model model) throws Exception {
		List<Function> functions = Global.getCurrentUser().getFunctions();
		
		//判断是否为根节点，如果是根节点则忽略，取根节点的子节点
		if(functions.size()==1&&functions.get(0).getFunction_type()==0){
			// 获取登录的bean
			model.addAttribute("list", functions.get(0).getChildren());
		}else{
			// 获取登录的bean
			model.addAttribute("list", Global.getCurrentUser().getFunctions());
		}
		// 登陆的信息回传页面
		model.addAttribute("user", Global.getCurrentUser());
		return "/index";
	}
	
	@RequestMapping(value = "logout", method = RequestMethod.GET)
	public String logout() {
		// 使用权限管理工具进行用户的退出，注销登录
		SecurityUtils.getSubject().logout(); // session
												// 会销毁，在SessionListener监听session销毁，清理权限缓存
		return "redirect:index.shtml";
	}

}
